package com.huasoft.servlet.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huasoft.dao.PageCondition;
import com.huasoft.dao.UserDao;
import com.huasoft.domain.User;

public class ListUserServlet {
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("listUsers", null);
		String name = request.getParameter("name");
		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
		Integer roleid = Integer.valueOf(request.getParameter("roleid"));
		PageCondition pageCondition = new PageCondition();
		if (request.getParameter("page") == null) {
			pageCondition.setPage(1);
		} else {
			Integer page = Integer.valueOf(request.getParameter("page"));
			pageCondition.setPage(page);
		}
		UserDao userDao = new UserDao();
		List<User> users = userDao
				.listUser(name, deptid, roleid, pageCondition);
		System.out.println(users.size());
		userDao.totalPage(name, deptid, roleid, pageCondition);
		request.setAttribute("listUsers", users);
		request.setAttribute("totalPage", pageCondition.getTotalPage());
		request.setAttribute("page", pageCondition.getPage());
		request.getRequestDispatcher("/users/users_list.jsp").forward(request,
				response);
	}
}
